<template>
	<z-paging ref="paging" v-model="dataList" @query="queryList"
		:loading-more-custom-style="{ paddingBottom: 'env(safe-area-inset-bottom)' }">
		<template #top>
			<view class="zpagin_top">
				<nav-bar :iconLeft="true" />
				<up-tabs :list="list" lineWidth="25" @change="tab_change" :scrollable="false"></up-tabs>
				<view class="search">
					<custom-search v-model="queryParams.search" @search="refreshList" placeholder="请输入入库单号/采购单号" />
				</view>
				<dui-float-ball :pathUrl="`${pageUrl}/compile`" v-if="$hasPerm(['50301'])" />
			</view>
		</template>
		<u-swipe-action>
			<u-swipe-action-item v-for="item in dataList" :key="item.id"
				:name="JSON.stringify(swipeActPer(options, item.status_display)) + '&' + item.id + '&' + item.code"
				:options="swipeActPer(options, item.status_display)"
				:disabled="swipeActPer(options, item.status_display, 'disabled')" @click="action">
				<uni-card margin="0" spacing="0" @click="goDetail(item, pageUrl, '50304')">
					<template v-slot:title>
						<uni-list>
							<uni-list-item :title="item.code" thumbSize="lg">
								<template v-slot:footer>
									<up-tag size="mini" :text="status_list[item.status_display].text" plain plainFill
										:type="status_list[item.status_display].type" />
								</template>
							</uni-list-item>
						</uni-list>
					</template>
					<view class="ctent">采购单号：{{ item.purchase_order_code }}</view>
					<view class="ctent">采购员：{{ item.purchase_full_name }}</view>
					<view class="con" style="align-items: normal;">
						<view class="ctent" style="flex: none;">供应商：</view>
						<view class="ctent">{{ item.supplier_info }}</view>
					</view>
					<view class="ctent">入库日期：{{ item.in_date }}</view>
					<view class="con" style="align-items: normal;">
						<view class="ctent" style="flex: none;">入库仓库：</view>
						<view class="ctent">{{ item.warehouse_info }}</view>
					</view>
					<view class="con">
						<view class="ctent">入库数量：{{ item.all_in_num }}</view>
						<view class="ctent">金额：{{ item.all_in_price }}</view>
					</view>
					<view class="con">
						<view class="ctent">制单人：{{ item.create_full_name }}</view>
						<view class="ctent">审核人：{{ item.apply_full_name || '' }}</view>
					</view>
					<view class="con" style="align-items: normal;">
						<view class="ctent" style="flex: none;">备注：</view>
						<view class="ctent">{{ item.desc }}</view>
					</view>
					<view class="con_button" v-if="$hasPerm(['50305'])" v-show="item.status_display == '待审核'"
						@click.stop="hanldAct(item.id, 2, '审核成功')"> 审核
					</view>
				</uni-card>
			</u-swipe-action-item>
		</u-swipe-action>
	</z-paging>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import puInApi from '../../../api/purchase/pu_in'
import { debounce } from '@/utils/index.js' // 用防抖优化请求频率（下面给出）
import { swipeActPer, goDetail } from '@/utils/permission'

uni.$bus.useBusOn('pu_in:refresh', refreshList)



const options = [
	{
		text: '修改', per: '50302',
		style: {
			backgroundColor: '#0081ff'
		}
	}, {
		text: '删除', per: '50303',
		style: {
			backgroundColor: '#f56c6c'
		}
	}
]
const pageUrl = '/views/purchase/pu_in'

//数据请求
const paging = ref(null);
const dataList = ref([]);
const queryParams = reactive({
	search: '',
	client__id: null,
	status: ''
})
//数据请求
function queryList(pageNo, pageSize) {
	const params = {
		page: pageNo,
		page_size: pageSize,
		...queryParams
	}
	puInApi.get_purchase_in(params).then((res) => {
		paging.value.complete(res.results);
	})
}

const tab_change = debounce(function (v) {
	queryParams.status = v.status;
	refreshList()
}, 500)

function hanldAct(id, status, text) {
	const params = {
		status, if_handle: 1
	}
	puInApi.patch_purchase_in(params, id).then(res => {
		uni.$u.toast(text)
		setTimeout(() => {
			refreshList()
		}, 500)
	})
}
//滑动操作按钮
function action(v) {
	const options = JSON.parse(v.name.split("&")[0])
	const id = v.name.split("&")[1]
	const delName = '单号为：' + v.name.split("&")[2]
	const index = v.index
	if (options[index].text == '修改') {
		uni.navigateTo({
			url: `${pageUrl}/compile?type=edit&id=${id}`
		})
	}
	if (options[index].text == '删除') {
		delete_type(id, delName)
	}
}

//删除操作
function delete_type(id, delName) {
	uni.showModal({
		title: '提示',
		content: `确认要删除单号为：【${delName}】的数据项吗？`,
		success: (res) => {
			if (res.confirm) {
				puInApi.delete_purchase_in(id).then((res) => {
					uni.$u.toast('删除成功')
					setTimeout(() => {
						refreshList()
					}, 500)
				})
			}
		}
	});
}
//编辑跳转刷新
function refreshList() {
	paging.value.reload();
}
const status_list = {
	'待审核': {
		text: '待审核',
		type: 'warning'
	},
	'已审核': {
		text: '已审核',
		type: 'success'
	},
	'已入库': {
		text: '已入库',
		type: 'primary'
	},

}
const list = [{
	name: '全部',
	status: ""
}, {
	name: '待审核',
	status: '1'
}, {
	name: '已审核',
	status: '2'
}, {
	name: '已入库',
	status: '3'
}]
</script>

<style lang="scss"></style>